<?php

function vendor($vendor='') {
	$vendor = VENDOR.$vendor;
	if(file_exists($vendor.'.php')) include $vendor.'.php';
	elseif(is_dir($vendor)) include_dir($vendor,true);
}

function view_partial($view) {
	$_view = VIEWS.CONTROLLER.DS.'_'.$view.'.php';
	if(file_exists($_view)) return $_view;
	elseif(file_exists(VIEWS.$view.'.php')) return VIEWS.$view.'.php';
	error_404(5,'View <strong>'.$view.'</strong> not found');
}

function render_partial($view, $content=array(), $params=array()) {
	$view = view_partial($view);
	ob_start();
	$i = 1;
	extract($params);
	foreach($content as $item) {
		if(is_array($item)) extract($item);
		if(is_object($item)) extract(get_object_vars($item));
		include($view); $i++;
	}
   	$output = ob_get_contents();
   	ob_end_clean();
   	return $output;
}

function h($where='') {
	header("Location: ".URL.$where);
}

function hr($extra='') {
	if(!isset($_SERVER['HTTP_REFERER'])) h();
	header("Location: ".$_SERVER['HTTP_REFERER'].$extra);
}

function redirect_to($action, $controller ='') {
	$action = $controller ? $controller.S.$action : CONTROLLER.S.$action;
	h($action);
}

function get_request(){
	return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')
		? 'ajax' : (count($_POST) === 0 ? 'get' : 'post') ;
}

function text2array($text) {
    $array = preg_split('#(\r\n?|\n)#', $text, -1, PREG_SPLIT_NO_EMPTY);
    $array = array_map('trim', $array);
    return $array;
}

?>